<?php
/************************************
*   熊店
*   @file:/server/bearshop/__include/data_model/mod_user.php
*   @intro:
*   @author:new_shop
*   @email:new_shop@163.com    
*   @date:Fri Feb 12 10:27:58 CST 2010
************************************/

class mod_product extends data_model 
{
    public function __construct($db_config)
    {
        parent::__construct($db_config);
        $this->_table = 'tl_product';
    }
    
    public function get_by_id($id)
    {
        $this->start();
        $this->set_condition('id='.$id);
        $rs = $this->fetch();
        if(!$rs)
            return false;
        $aPhotolist = unserialize($this->_data['photo_list']);
        $this->_data['photo_list'] = is_array($aPhotolist) ? $aPhotolist : array();
        return true;
    }
    
    public function get_by_sn($sn)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('sn="'.$sn.'"');
        $rs = $this->fetch();
        if(!$rs)
            return false;
        if($this->get_num_rows() > 0)
        {
            $aPhotolist = unserialize($this->_data['photo_list']);
            $this->_data['photo_list'] = is_array($aPhotolist) ? $aPhotolist : array();
        }
        return true;
    }
    
    public function create_product($data)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_array($data);
        $this->set('ctime' , time());
        $this->set('cdate' , date('Y-m-d'));
        $this->set('photo_list' , '');
        return $this->insert();
    }
    
    public function edit_product($id , $data)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$id);
        $this->set_array($data);
        return $this->update();
    }
    
    public function list_product(&$count , $page = 1 , $pagesize = 10 , $condition = '' , $order = 'id DESC,sn DESC')
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition($condition);
        
        $this->fetch_count($count);
        $start = $this->get_limit_start($page , $pagesize);
        $rs = $this->fetch_multi('',$order , $start , $pagesize);
        if(!$rs)
            return false;
            
        if($this->get_num_rows()>0)
        {
            foreach ($this->_data as $k => $row)
            {
                $this->_data[$k]['photo_list'] = unserialize($row['photo_list']);
            }
        }
        return true;
    }
    
    public function set_value_by_id($id , $field , $value)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$id);
        $this->set($field , $value);
        return $this->update();
    }
    
    public function set_value_in_ids($aId , $field , $value)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id in ('.implode(',' , $aId).')');
        $this->set($field , $value);
        return $this->update();
    }
    
    
    public function append_photo_list($id , $aPid)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$id);
        $rs = $this->fetch('photo_list');
        if(!$rs)
            return false;
            
        $photo_list = $this->get('photo_list');
        $aPidDb = $photo_list ? unserialize($photo_list) : array();
        
        
        $aPid = array_flip($aPid);
        $aPidDb = array_merge($aPidDb , $aPid);

        $this->set('photo_list' , serialize($aPidDb));
        return $this->update();
    }
    
    public function delete_photo_list($id , $sPid , &$is_cover)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$id);
        $rs = $this->fetch('photo_list');
        if(!$rs)
            return false;
            
        $photo_list = $this->get('photo_list');
        
        $aPidDb = $photo_list ? unserialize($photo_list) : array();
        
        
        $is_cover = $aPidDb[$sPid]['is_cover'] == 1 ? true : false;
        unset($aPidDb[$sPid]);
        
        $this->set('photo_list' , serialize($aPidDb));
        return $this->update();
    }
    
    public function set_photo_cover($id , $sPid)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$id);
        
        $this->fetch('photo_list');
        $aPidDb = unserialize($this->get('photo_list'));
        
        foreach ((array)$aPidDb as $k => $v)
        {
            if($v['is_cover'])
                unset($aPidDb[$k]['is_cover']);
        }
        if(is_array($aPidDb[$sPid]))
            $aPidDb[$sPid]['is_cover'] = 1;
        else 
            $aPidDb[$sPid] = array('is_cover' => 1);
        
        $this->set('photo_list' , serialize($aPidDb));
        return $this->update();
        
    }

    public function list_by_id($aId , $field = array('name'))
    {
        $this->start();
        $this->set_condition('id in ('.implode(',' , $aId).')');
        return $this->fetch_multi($field);
    }
    
    public function list_by_sn($aSn)
    {
        $this->start();
        $this->set_condition('sn in ("'.implode('","' , $aSn).'")');
        return $this->fetch_multi();
    }
    
    public function list_new_product($pagesize = 5)
    {
        $this->start();
        $this->set_condition('stat='.K_COMMON_YES);
        return $this->fetch_multi(null , 'id DESC' , 0 , $pagesize);
    }

    public function list_date(&$total , $page=1 , $pagesize = 5)
    {
        $this->start();
        $sql = 'SELECT count(distinct(cdate)) n FROM '.K5_DATAMOD_TB.' WHERE stat='.K_COMMON_YES;
        $this->fetch_by_sql($sql);
        $total = $this->_data['n'];
        
        $start = $this->get_limit_start($page , $pagesize);
        $sql = 'SELECT distinct(cdate) cdate FROM '.K5_DATAMOD_TB.' WHERE stat='.K_COMMON_YES.' ORDER BY cdate DESC limit '.$start.' , '.$pagesize;
        return $this->fetch_by_sql($sql , true);
    }
    
    public function list_by_date($aDate)
    {
        $this->start();
        $s_in = '"'.implode('","' , $aDate).'"';
        $this->set_condition('cdate in ('.$s_in.') AND stat='.K_COMMON_YES);
        return $this->fetch_multi();
    }
    
    public function search_by_name(&$count , $is_eng , $key , $class = 0 )
    {
        $this->start();
        
        $condition = 'stat in ('.K_COMMON_YES.','.K_COMMON_LOCK.') AND ';
        if($class > 0)
            $condition .= 'class_id = '.$class.' AND ';
        $condition .= '('.($is_eng ? 'eng_name' : 'name') ." like '%".$key."%'";
        $condition .= " OR sn like '%".$key."%')";
        
        $this->set_condition($condition);
        
        return $this->fetch_multi(array() , 'id DESC');
    }
    public function search_by_sn($key)
    {
        $this->start();
        
        $condition = '';
        $condition .= "sn like '%".$key."%'";
        
        $this->set_condition($condition);
        return $this->fetch_multi(array() , 'id DESC');
    }
}
?>